package com.linxs.topcham.intefaces.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.linxs.topcham.application.channel.ChannelService;
import com.linxs.topcham.application.commodity.CommodityService;
import com.linxs.topcham.application.company.CompanyService;
import com.linxs.topcham.application.resources.ResourcesService;
import com.linxs.topcham.domain.channel.Channel;

/**
 * 
 * @author Linxs
 * 
 */
@Controller
public class HomeController {

	@Autowired
	private CommodityService commodityService;
	@Autowired
	private ChannelService channelService;
	@Autowired
	private ResourcesService resourcesService;
	@Autowired
	private CompanyService companyService;

	@RequestMapping(value = { "/home", "/" }, method = RequestMethod.GET)
	public String linxs(Model model, HttpServletRequest request) {
		model.addAttribute("channelTop", channelService.queryTop());
		model.addAttribute("logo", resourcesService.getLogo());
		model.addAttribute("focusPictures", resourcesService.queryFocusPictures());
		model.addAttribute("company", companyService.getCompany());
		return "home"; 
		
	}

	@RequestMapping(value = { "/language/{language}" }, method = RequestMethod.GET)
	public String language(Model model, @PathVariable("language") String language, HttpServletRequest request) {
		request.getSession().invalidate();
		if ("english".equals(language)) {
			request.getSession().setAttribute("language", "english");
		}
		if ("espana".equals(language)) {
			request.getSession().setAttribute("language", "espana");
		}
		if("chinese".equals(language)) {
			request.getSession().setAttribute("language", "chinese");
		}
		
		model.addAttribute("channelTop", channelService.queryTop());
		model.addAttribute("logo", resourcesService.getLogo());
		model.addAttribute("focusPictures", resourcesService.queryFocusPictures());
		model.addAttribute("company", companyService.getCompany());
		
		return "home";
	}

	@RequestMapping(value = "/channel/{id}/", method = RequestMethod.GET)
	public String channel(Model model, @PathVariable("id") int id) {
		Channel channel = channelService.get(id);
		if (channel.getFather() != null) {
			channel.setChildren(channelService.queryChildById(channel.getFather().getId()));
		}

		model.addAttribute("channel", channel);
		model.addAttribute("channelTop", channelService.queryTop());
		model.addAttribute("logo", resourcesService.getLogo());
		model.addAttribute("company", companyService.getCompany());

		return "channel";
	}

}
